package com.bkgy.modular.tbdecsionrule.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.modular.tbdecsionrule.dao.TbDecsionRuleDao;
import com.bkgy.modular.tbdecsionrule.entity.TbDecsionRule;
import com.bkgy.modular.tbdecsionrule.service.TbDecsionRuleService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.ExcelUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Service
public class TbDecsionRuleServiceImpl extends ServiceImpl<TbDecsionRuleDao,TbDecsionRule> implements TbDecsionRuleService {

    @Resource
    private TbDecsionRuleDao tbDecsionRuleDao;

    @Override
    public ResultData insertOne(String data, Long userId, String sname) {
        try{
            TbDecsionRule tbDecsionRule = JSON.parseObject(data,TbDecsionRule.class);
            this.save(tbDecsionRule);
            return ResultData.ResultDataSuccessSelf("添加数据成功", null);
        }catch(Exception e){
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("添加失败，错误信息为" + e.getMessage(), null);
        }
    }

    @Override
    public ResultData deleteOneById(String data) {
        try{
            TbDecsionRule tbDecsionRule = JSON.parseObject(data,TbDecsionRule.class);
            this.removeById(tbDecsionRule.getIndocno());
            return ResultData.ResultDataSuccessSelf("删除数据数据成功", null);
        }catch(Exception e){
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null);
        }
    }

    @Override
    public ResultData deleteManyByIds(String data) {
        try {
            List<Integer> list = JSON.parseArray(data, Integer.class);
            this.removeByIds(list);
            return ResultData.ResultDataSuccessSelf("删除数据数据成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null);
        }
    }

    @Override
    public ResultData updateDataTbDecsionRule(JSONObject jsonObject) {
        try {
            String dataId = jsonObject.getString("dataId");
            LambdaQueryWrapper<TbDecsionRule> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(TbDecsionRule::getOrder,dataId);
            String data = jsonObject.getJSONObject("data").toJSONString();
            TbDecsionRule tbDecsionRule = JSON.parseObject(data,TbDecsionRule.class);
            tbDecsionRuleDao.update(tbDecsionRule,queryWrapper);
            return ResultData.ResultDataSuccessSelf("修改成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("修改失败，错误信息为" + e.getMessage(), null);
        }
    }

    /*@Override
    public ResultData updateDataTbDecsionRule(String data, Long userId, String sname) {
        try{
            JSONObject jsonObject = JSONObject.parseObject(data);
            String data1 = jsonObject.getJSONObject("data").toJSONString();
            TbDecsionRule tbDecsionRule = JSON.parseObject(data1,TbDecsionRule.class);
            this.updateById(tbDecsionRule);
            return ResultData.ResultDataSuccessSelf("修改成功", null);
        }catch(Exception e){
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("修改失败，错误信息为" + e.getMessage(), null);
        }
    }*/

    @Override
    public ResultData findDataTbDecsionRuleByPage(String data) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Integer pageIndex = jsonObject.getInteger("pageIndex");
            Integer pageSize = jsonObject.getInteger("pageSize");
            //查询条件暂无，后续需要再根据需求添加
            JSONObject condition = jsonObject.getJSONObject("condition");

            String process_rule_variable = condition.getString("process_rule_variable");
            String variable_description = condition.getString("variable_description");


            LambdaQueryWrapper<TbDecsionRule> queryWrapper = new LambdaQueryWrapper<>();

            if (!StringUtils.isEmpty(process_rule_variable)) {
                queryWrapper.eq(TbDecsionRule::getOrder,process_rule_variable);
            }

            if (!StringUtils.isEmpty(variable_description)) {
                queryWrapper.eq(TbDecsionRule::getThk_grade,variable_description);
            }
            if(null == pageIndex || null == pageSize){
                return ResultData.ResultDataFaultSelf("分页参数没有传", null);
            }
            Page<TbDecsionRule> page = new Page<>(pageIndex,pageSize);
            Page<TbDecsionRule> tbDecsionRulePage = this.page(page, queryWrapper);
            return ResultData.ResultDataSuccess(tbDecsionRulePage.getRecords(), (int) tbDecsionRulePage.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }

    @Override
    public void exportList(JSONObject jsonObject, HttpServletResponse response) {

        // 接收传入参数
        // Integer pageSize = jsonObject.getInteger("pageSize"); // 每页多少条
        // Integer pageCount = jsonObject.getInteger("pageCount"); // 总页数
        String fileNames = jsonObject.getString("fileName"); // 文件名称
        JSONObject condition = jsonObject.getJSONObject("condition"); // 查询条件

        LambdaQueryWrapper<TbDecsionRule> queryWrapper = new LambdaQueryWrapper<>();

        String process_rule_variable = condition.getString("process_rule_variable");
        String variable_description = condition.getString("variable_description");

        if (!StringUtils.isEmpty(process_rule_variable)) {
            queryWrapper.eq(TbDecsionRule::getOrder,process_rule_variable);
        }

        if (!StringUtils.isEmpty(variable_description)) {
            queryWrapper.eq(TbDecsionRule::getThk_grade,variable_description);
        }

        // 创建表格数据集合
        List<TbDecsionRule> listExcel = new ArrayList<>();
        // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
        List<TbDecsionRule> data = tbDecsionRuleDao.selectList(queryWrapper);
        // 判断service查询出的数据集合data是否为空
        if (CollectionUtils.isEmpty(data)) {
            return;
        }

        ExcelUtils.downloadExcel(fileNames,response,TbDecsionRule.class,data);
    }


}
